package xyz.jiang.common.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Slf4j
@Aspect
@Component
public class ServiceLogAspect {

    @Around("execution(* xyz.jiang..service.impl..*.*(..))")
    public Object recordTimeLog(ProceedingJoinPoint point) throws Throwable {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Object proceed = point.proceed();
        stopWatch.stop();
        String name = point.getSignature().getName();
        String className = point.getTarget().getClass().toString();
        if(stopWatch.getTotalTimeMillis() >= 3000) {
            log.warn("【{}.{}】执行耗时：{}ms", className, name, stopWatch.getTotalTimeMillis());
        } else {
            log.info("【{}.{}】执行耗时：{}ms", className, name, stopWatch.getTotalTimeMillis());
        }
        return proceed;
    }
}
